#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: compile.py
# Description: 
#   compile(source, filename, mode, flags=0, dont_inherit=False)    將 source 字串編譯為可執行物件
#   filename 為對應的檔案名稱字串，一般用空字串即可， 
#   mode 有三種，分別是單一陳述的 "single" ，複合陳述的 "exec" ，運算式的 "eval" ，
#   因此執行編譯後的 code 物件，需以另外兩個內建函數 exec() 與 eval() 呼叫。
# Edit History: 
#   2011-09-08    File created.
#===============================================================================

#===============================================================================
# 1. exec
#===============================================================================
print("start")
s = "for i in range(8): print(i)"
t = compile(s, "", "exec")
exec(t)
print("end")


#===============================================================================
# 2. eval
#===============================================================================
print("start")
s = "print('hello')"
t = compile(s, "", "single")
eval(t)
print("end")

